package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;;
import com.iwomy.secureplat.platform.dto.BasicAuthorizationRecordDTO;
import com.iwomy.secureplat.platform.dto.DeviceModelRequestDTO;
import com.iwomy.secureplat.platform.entity.BasicAuthorizationRecordEntity;
import com.iwomy.secureplat.platform.entity.BasicDeviceModelEntity;
import com.iwomy.secureplat.platform.service.BasicAuthorizationRecordService;
import com.iwomy.secureplat.platform.service.BasicDeviceModelService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 授权记录表
 *
 * @author pig
 * @date 2023-11-13 18:06:03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicAuthorizationRecord")
@Tag(description = "basicAuthorizationRecord", name = "授权记录表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicAuthorizationRecordController {

	private final BasicAuthorizationRecordService basicAuthorizationRecordService;

	private final RedisTemplate<String, Object> redisTemplate;

	private final BasicDeviceModelService deviceModelService;

	/**
	 * 分页查询
	 *
	 * @param page                  分页对象
	 * @param basicAuthorizationDTO 授权记录表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicAuthorizationRecord_view')")
	public R getBasicAuthorizationRecordPage(@ParameterObject Page page, @ParameterObject BasicAuthorizationRecordDTO basicAuthorizationDTO) {
		return R.ok(basicAuthorizationRecordService.getBasicAuthorizationRecordPage(page, basicAuthorizationDTO));
	}


	/**
	 * 通过id查询授权记录表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicAuthorizationRecord_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicAuthorizationRecordService.getById(id));
	}

	/**
	 * 新增授权记录表
	 *
	 * @param deviceModelRequestDTO 授权记录表
	 * @return R
	 */
	@Operation(summary = "新增授权记录表", description = "新增授权记录表")
	@SysLog("新增授权记录表")
	@PostMapping
	public R save(@RequestBody DeviceModelRequestDTO deviceModelRequestDTO) {
		return basicAuthorizationRecordService.newlyAdded(deviceModelRequestDTO);
	}


	/**
	 * 校验验证码
	 *
	 * @param code 实体
	 */
	@GetMapping("/checkCode")
	public R checkCode(@RequestParam String code) {
		String captchaValue = (String) redisTemplate.opsForValue().get("captcha");
		if (captchaValue != null && captchaValue.equals(code.toLowerCase())) {
			return R.ok();
		} else {
			return R.failed("验证码校验不通过");
		}
	}

	/**
	 * 修改授权记录表
	 *
	 * @param basicAuthorizationRecord 授权记录表
	 * @return R
	 */
	@Operation(summary = "修改授权记录表", description = "修改授权记录表")
	@SysLog("修改授权记录表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicAuthorizationRecord_edit')")
	public R updateById(@RequestBody BasicAuthorizationRecordEntity basicAuthorizationRecord) {
		return R.ok(basicAuthorizationRecordService.updateById(basicAuthorizationRecord));
	}

	/**
	 * 通过id删除授权记录表
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除授权记录表", description = "通过id删除授权记录表")
	@SysLog("通过id删除授权记录表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicAuthorizationRecord_del')")
	public R removeById(@RequestBody Long[] ids) {
		return basicAuthorizationRecordService.newlyRemoveBatchByIds(ids);
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicAuthorizationRecord 查询条件
	 * @param ids                      导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicAuthorizationRecord_export')")
	public List<BasicAuthorizationRecordEntity> export(BasicAuthorizationRecordEntity basicAuthorizationRecord, Long[] ids) {
		return basicAuthorizationRecordService.list(Wrappers.lambdaQuery(basicAuthorizationRecord).in(ArrayUtil.isNotEmpty(ids), BasicAuthorizationRecordEntity::getId, ids));
	}

	/**
	 * 分页查询
	 * @param basicDeviceModel 授权记录中设备型号列表
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/getAuthorizationModelList" )
//	@PreAuthorize("@pms.hasPermission('platform_basicAuthorizationRecord_view')" )
	public R getAuthorizationModelList( @ParameterObject BasicDeviceModelEntity basicDeviceModel) {
		return R.ok(deviceModelService.getAuthorizationModelList(basicDeviceModel));
	}

}